package com.hoopchina.ui.details;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

/**
 * 新闻收藏界面数据库Engine
 * 
 * @author Neusoft
 *
 */
public class FavoritesEngine {	
	private Context mContext;
	private SQLiteDatabase db;
	
	public FavoritesEngine(Context context){
		mContext = context;
		FavoritesDatabaseHelper dbHelper = new FavoritesDatabaseHelper(mContext);
		db = dbHelper.getWritableDatabase();
	}
	
	public void CreateTable(String tablename) {
		String sql = "CREATE TABLE IF NOT EXISTS "
				+ tablename
				+ " (_id INTEGER PRIMARY KEY ,url VARCHAR, title VARCHAR, abs VARCHAR, author VARCHAR,  time VARCHAR, imgurl VARCHAR)";
		db.beginTransaction();
		try {
			db.execSQL(sql);
		} catch (SQLException e) {

		} catch (IllegalArgumentException e1) {

		}
		db.endTransaction();
	}
	
	/**
	 * 打开数据库
	 * @param tablename 数据库的名称
     * 
	 */
	public void openDatabase(String tablename) {
		try {
			db = SQLiteDatabase.openDatabase(tablename, null,
					SQLiteDatabase.OPEN_READWRITE);
		} catch (SQLException e) {

		} catch (IllegalArgumentException e1) {

		}
	}
	
	public Cursor queryAll() {
		Cursor cursor = null;
		String sql = "SELECT * FROM FavoritesDatabase";
		try {
			//cursor = db.rawQuery(sql, null);
            //数据库根据id按倒序排列
			cursor = db.rawQuery("select *from FavoritesDatabase order by _id desc",null);
//			cursor = db.query("FavoritesDatabase",
//
//                    new String[]{"url","title","abs","author","time","imgurl"}, 
//
//                    null, 
//
//                    null, null, null, "_id desc");



			if (cursor == null) {
			}
			// Toast.makeText(null, "query all error",
			// Toast.LENGTH_SHORT).show();
		} catch (SQLException e) {

		} catch (IllegalArgumentException e1) {

		}

		return cursor;
	}
	
	/**
	 * 根据新闻标题查找数据
	 * @param title 新闻标题
     * 
	 */
	public Cursor queryByTitle(String title) {
		Cursor cursor = null;
		try {
			cursor = db.query("FavoritesDatabase", null, "title=?",
					new String[] { title }, null, null, null);
			if (cursor == null) {
			}
			// Toast.makeText(null, "querybyid error",
			// Toast.LENGTH_SHORT).show();
		} catch (SQLException e) {

		} catch (IllegalArgumentException e1) {

		}

		return cursor;
	}
	
	/**
	 * 根据新闻源地址查找数据
	 * @param url 新闻源地址
     * 
	 */
	public Cursor queryByUrl(String url) {
		Cursor cursor = null;
		try {
			cursor = db.query("FavoritesDatabase", null, "url=?",
					new String[] { url }, null, null, null);
			if (cursor == null) {
			}
			// Toast.makeText(null, "querybyid error",
			// Toast.LENGTH_SHORT).show();
		} catch (SQLException e) {

		} catch (IllegalArgumentException e1) {

		}

		return cursor;
	}
	
	/**
	 * 向数据库插入收据
	 * @param url 新闻源地址
	 * @param title 新闻标题
	 * @param abs 新闻摘要
	 * @param author 新闻来源
	 * @param time 新闻时间
	 * @param imgUrl 新闻图片地址
	 */
	public void insert(String url, String title, String abs, String author, String time ,String imgUrl) { 
		try {
			ContentValues value = new ContentValues();
			value.put("url", url);
			value.put("title", title);
			value.put("abs", abs);
			value.put("author", author);
			value.put("time", time);
			value.put("imgurl", imgUrl);

			long result = db.insert("FavoritesDatabase", null, value);

			if (result == -1) {
			}
			// Toast.makeText(null, "insert error", Toast.LENGTH_SHORT).show();
		} catch (SQLException e) { 
		       
		    } 
		}
	
	/**
	 * 根据新闻标题从数据库删除数据
	 * @param title 新闻标题
	 */
	public void delete(String title) { 
		try {
			int rows = db.delete("FavoritesDatabase", "title=?",
					new String[] { title });
			if (rows == 0) {
			}
			// Toast.makeText(null, "delete error", Toast.LENGTH_SHORT).show();
		} catch (SQLException e) {
		    	 
		    } 
		}
	
	public void updateState(String tablename, String id, String state) {
		try {
			ContentValues values = new ContentValues();
			values.put("state", state);
			db.update(tablename, values, "id=?", new String[] { id });
		} catch (SQLException e) {

		} catch (IllegalArgumentException e1) {

		}
	}
	
	
	public void dropTable(String tablename) {
		String sql = "DROP TABLE IF EXISTS " + tablename;
		try {
			db.execSQL(sql);
		} catch (SQLException e) {

		} catch (IllegalArgumentException e1) {

		}
	}
	
	/**
	 * 关闭数据库
	 */
	public void closeDatabase() {
		db.close();
	}
}
